
import os, time, store_credit
from Tkinter import *

class main():
    def __init__(self,parent,root):
        
        self.root = root
        self.dh = parent.dh
        self.consignor = None
        self.root.title('Add or Edit Consignor') 
        self.mainframe = Frame(self.root,padx=20,pady=20 ) 
        self.mainframe.grid(column=0, row=0, sticky = (N, W, E ,S))
        self.mainframe.columnconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=1) 
        self.creditLedger = store_credit.main(parent)
        next_num = self.dh.next_available_consignor_number()
        self.num = StringVar()
        self.num.set(next_num)
        self.name = StringVar()
        self.addy1 = StringVar()
        self.addy2 = StringVar()
        self.addy3 = StringVar()
        self.phone = StringVar()
        self.date = StringVar()
        self.cur_owed_dol = StringVar()
        self.cur_com = StringVar()     
        self.old_cur_owed = StringVar()
        self.old_cur_owed.set('Current amount owed: $0.00')

        self.payAmount = StringVar()
        self.payAmount.set('0.00')
        self.payFlag = IntVar()
       
        self.email = StringVar()
        self.timestamp = time.struct_time(time.localtime())
        day = self.timestamp.tm_mday
        month = self.timestamp.tm_mon
        year = self.timestamp.tm_year        
        self.form_date = '%s/%s/%s' % (month,day,year)
        self.date.set(self.form_date)
        self.status = StringVar()
        self.status.set('Waiting for input')
        
        
        numLabel = Label(self.root,text= 'Consignor Number  : ').grid(row=0,column=0,sticky=W)
        findByNum = Button(self.root, text='Search',command=self.readConsignorNum, width=10).grid(row=0,column=2)
        numEntry = Entry(self.root,textvariable=self.num,width=4)
        numEntry.grid(row=0,column=1,sticky=W)
        numEntry.bind('<Return>',self.readConsignorNum)
        numEntry.bind('<KP_Enter>',self.readConsignorNum)
        
        nameLabel = Label(self.root,text='Consignor Name     : ').grid(row=1,column=0,sticky=W)
        nameEntry = Entry(self.root,textvariable=self.name,width=40)
        nameEntry.grid(row=1,column=1)
        nameEntry.bind('<Return>',self.readConsignorName)
        nameEntry.bind('<KP_Enter>',self.readConsignorName)
        findByName = Button(self.root, text='Search',command=self.readConsignorName, width=10).grid(row=1,column=2)
        add1Label = Label(self.root,text='Address 1               : ').grid(row=2,column=0,sticky=W)
        add2Label = Label(self.root,text='Address 2               : ').grid(row=3,column=0,sticky=W)
        add3Label = Label(self.root,text='Address 3               : ').grid(row=4,column=0,sticky=W)
        add1Entry = Entry(self.root,textvariable=self.addy1,width=40).grid(row=2,column=1)
        add2Entry = Entry(self.root,textvariable=self.addy2,width=40).grid(row=3,column=1)
        add3Entry = Entry(self.root,textvariable=self.addy3,width=40).grid(row=4,column=1)
        phoneLabel = Label(self.root,text='Phone Number       : ').grid(row=5,column=0,sticky=W)
        phoneEntry = Entry(self.root,textvariable=self.phone,width=40).grid(row=5,column=1)
        emailLabel = Label(self.root,text='Email Address         : ').grid(row=6,column=0,sticky=W)
        emailEntry = Entry(self.root,textvariable=self.email,width=40).grid(row=6,column=1)        
        dateLabel = Label(self.root,text='Consign Date          : ').grid(row=7,column=0,sticky=W)       
        dateEntry = Entry(self.root,textvariable=self.date,width=40).grid(row=7,column=1)
        
        spc1 = Label(self.root, text = ' ').grid(row=8,column=0)
        
        curr_amountLabel = Label(self.root, textvariable = self.old_cur_owed).grid(row=10,column=0,columnspan=3,sticky=W)
               
        payLabel = Label(self.root, text = 'Pay this amount       :$').grid(row=11,column=0,sticky=W)
        payEntry = Entry(self.root, textvariable=self.payAmount,width=10)
        payEntry.grid(row=11,column=1,sticky=W)
       
        comLabel = Label(self.root, text = 'Comments                : ').grid(row=12,column=0,sticky=W)
        comEntry = Entry(self.root, textvariable = self.cur_com, width=40)
        comEntry.grid(row=12,column=1)
        comEntry.bind('<Return>', self.finalize)
        comEntry.bind('<KP_Enter>', self.finalize)
        
        self.payFlag.set(1)
        payMethod = [('CHECK',1),('CASH',2),('STORE CREDIT',3)]
        radioframe = Frame(self.root)
        radioframe.grid(row=13,column=0,columnspan=2)
        i=0
        for text, mode in payMethod:
            b = Radiobutton(radioframe, text=text, variable=self.payFlag, value=mode)
            b.grid(row=0,column=i)
            i+=1   
                
        doneButton = Button(self.root,text=' Done ', command=self.finalize).grid(row=14,column=1,sticky=E)
        statusLabel = Label(self.root, textvariable=self.status).grid(row=15,column=0,columnspan=2)

    def assignVars(self,cons):
        self.consignor = cons
        self.num.set(cons.number)
        self.name.set(cons.full_name)
        self.addy1.set(cons.addy1)
        self.addy2.set(cons.addy2)
        self.addy3.set(cons.addy3)
        self.phone.set(cons.phone_number)
        self.email.set(cons.email_address)
        self.date.set(cons.consign_date)
        self.cur_owed_dol.set(cons.last_owed_amount)
        self.old_cur_owed.set('Current amount owed: $%s: %s' % (cons.last_owed_amount,cons.last_owed_comments))
    
    
    def readConsignorNum(self,event=None):
        num = self.num.get()
        if num in self.dh.cons_dict.keys():
            cons = self.dh.cons_dict[num]
            self.assignVars(cons)
        else:
            self.status.set('ERROR: Consignor number not found')
            
    def readConsignorName(self,event=None):
        name = self.name.get()
        cons = self.dh.find_consignor_by_name(name)
            
        if cons == None:
            self.status.set('ERROR: No match found')
        else:
            self.status.set('Waiting for input')
            self.assignVars(cons)
           
                    
    def updateOwed(self,event=None):     
        pay_amount = self.payAmount.get()
        if pay_amount != '0.00':
            
            payFlag = self.payFlag.get()
            payMethods = {1:'CHECK',2:'CASH',3:'STORE CREDIT'}
            payMethod = payMethods[payFlag]
            num = self.num.get()
            comments = self.cur_com.get()+' - $%s %s - %s' % (pay_amount,payMethod,self.form_date)
            pay_amount = self.payAmount.get()
            self.dh.cons_dict[num].payout(pay_amount,comments) 
            if payFlag == 3:
                self.creditLedger.credit_account(self.num.get(),pay_amount,self.name.get())
  
        
    
    def finalize(self,event=None):
        if self.consignor == None:
            self.consignor = self.dh.add_new_consignor()        
        self.updateOwed()
        name = self.name.get()
        if len(name) > 1:
            num = self.num.get()
            cons = self.consignor
            cons.full_name = self.name.get()
            cons.addy1 = self.addy1.get()
            cons.addy2 = self.addy2.get()
            cons.addy3 = self.addy3.get()
            cons.email_address = self.email.get()
            cons.phone_number = self.phone.get()
            cons.date = self.date.get()
            cons.save() 
            self.root.destroy()
        else:
            self.status.set('Something is entered incorrectly. Check all fields for completion')            
        

def start(parent,root):
    ins = main(parent,root)
